(function() {
	var skill = {
		save: function() {
			db.update(config.dbname.tag, this._id, this);
		},
		destroy: function() {
			db.remove(config.dbname.tag, this._id);
		},
		//修改对象，同构造函数，需要确保每一个新值的合法
		setInfo: function(info) {
			//校验联盟属性
			//联盟名称
			if (!helper.folder_name(info.league_name)) {
				throwCusError("联盟名命名不规范")
			}
			if (db.find_by_id(db_table_name, info.league_name)) {
				throwCusError("已经存在名字相同的联盟名称")
			}
			self.league_name = info.league_name;
			//联盟介绍
			self.intro = info.intro ? String(info.intro) : "";
			if (self.intro.length > config.leagueDescriptionLength) {
				throwCusError("联盟简介长度超出长度限制");
			}
			//无权修改联盟状态
			//状态：未审核，审核中，审核通过、审核未通过、冻结（被举报并查出有问题）
			// self.status = info.status;//"未审核";
		},
		//代码片段，获取指定活动
		_getActivity: function(activity_id) {
			var activitys = this.activitys || [];
			if (activitys.indexOf(activity_id) === -1) {
				throwCusError("活动不属于指定联盟")
			}
			var activity = ActivityCon.get(activity_id);
			if (!activity) {
				throwCusError("找不到指定活动")
			}
			return activity;
		},
		//获取活动集合
		getActivitys: function(num, page) {
			var activitys = this.activitys || [];
			activitys = data_cut.page(activitys, num, page);
			activitys.forEach(function(activity_id, index) {
				activitys[index] = ActivityCon.get(activity_id);
			});
			return activitys;
		},
		//非安全，添加活动
		_addActivity: function(activity_id) {
			var activitys = this.activitys || (this.activitys = []);
			if (activitys.indexOf(activity_id) !== -1) {
				throwCusError("活动已经存在")
			}
			var activity = ActivityCon.get(activity_id);
			if (!activity) {
				throwCusError("找不到指定活动")
			}
			activitys.push(activity_id);
			this.save();
		},
		//非安全，修改活动
		_updateActivity: function(activity_id, activituInfo) {
			var activity = this._getActivity(activity_id);
			//混合新旧数据
			activity.setInfo(activituInfo);
		},
		//非安全，删除活动
		_removeActivity: function(activity_id) {
			var activitys = this.activitys || (this.activitys = []);
			var index = activitys.indexOf(activity_id);
			if (index === -1) {
				throwCusError("活动不属于指定联盟")
			}
			var activity = ActivityCon.get(activity_id);
			if (!activity) {
				//TODO:这里数据出错，理应该删除冗余数据
				throwCusError("找不到指定活动")
			}
			activity.destroy();
			activitys.splice(index, 1);
			this.save();
		},
		// //如果商家默认让自己所有的商品全部运行用于销售用途，则这个方法没有实际意义
		// //获取可做联盟销售的商品
		// getMarketGoods:function () {
		// 	// body...
		// },
		// //添加可做销售的商品
		// _addMarketGoods:function () {
		// 	// body...
		// },
		// //移除可做联盟销售的商品
		// _removeMarketGoods:function () {
		// 	// body...
		// },
		//代码片段，获取该联盟的指定促销方案
		_getMarket: function(market_id) {
			var markets = this.markets || [];
			if (markets.indexOf(market_id) === -1) {
				throwCusError("促销方案不属于属于指定联盟")
			}
			var market = MarketCon.get(market_id);
			if (!market) {
				throwCusError("找不到指定促销方案")
			}
			return market
		},
		//获取促销方案集合
		getMarkets: function(num, page) {
			var markets = this.markets || [];
			markets = data_cut.page(markets, num, page);
			markets.forEach(function(market_id, index) {
				markets[index] = MarketCon.get(market_id);
			});
			return markets;
		},
		//非安全，添加促销方案
		_addMarket: function(market_id) {
			var markets = this.markets || (this.markets = []);
			if (markets.indexOf(market_id) !== -1) {
				throwCusError("促销方案已经属于指定联盟")
			}
			var market = MarketCon.get(market_id);
			if (!market) {
				throwCusError("找不到指定促销方案")
			}
			markets.push(market_id);
			this.save();
		},
		//非安全，修改促销方案
		_updateMarket: function(market_id, marketInfo) {
			var market = this._getMarket(market_id);
			market.setInfo(marketInfo);
		},
		//非安全，删除促销方案
		_removeMarket: function(market_id) {
			var market = this._getMarket(market_id);
			this.markets = $Array.remove(this.markets, market_id);
			this.save();
		},
		//获取联盟中的商家
		getBuses: function(num, page) {
			var merchants = this.merchants || [];
			var admins = this.admins || [];
			merchants = data_cut.page(merchants, num, page);
			merchants.forEach(function(bus_id, index) {
				var bus = BusCon.get(bus_id);
				bus = $Object.clone(bus);
				if (admins.indexOf(bus._id) !== -1) {
					bus.league_role = "admin"
				} else {
					bus.league_role = "merchant"
				}
				merchants[index] = bus;
			});
			//添加盟主
			var avalon = BusCon.get(this.avalon);
			avalon = $Object.clone(avalon);
			avalon.league_role = "avalon";
			merchants.push(avalon);
			return merchants;
		},
		//获取盟主，将bus转化为avalon
		getAvalon: function(bus_id) {
			if (bus_id !== this.avalon) {
				throwCusError("权限不足")
			}
			var bus = BusCon.get(bus_id);
			var avalone = $Object.extend(bus, LeagueAvaloneProto);
			return avalone;
		},
		//获取管理员，将bus转化为admin
		getAdmin: function(bus_id) {
			var admins = this.admins || [];
			if (admins.indexOf(bus_id) === -1) {
				throwCusError("权限不足")
			}
			var bus = BusCon.get(bus_id);
			var admin = $Object.extend(bus, LeagueAdminProto);
			return admin;
		},
		//获取联盟成员，将bus转化为merchant
		getMerchant: function(bus_id) {
			var merchants = this.merchants || [];
			if (merchants.indexOf(bus_id) === -1) {
				throwCusError("权限不足")
			}
			var bus = BusCon.get(bus_id);
			var merchant = $Object.extend(bus, LeagueMerchantProto);
			return merchant;
		},
		//获取指定bus在联盟中的对应角色
		getRole: function(bus_id) {
			try {
				var result = this.getAvalon(bus_id);
			} catch (e) {
				try {
					result = this.getAdmin(bus_id);
				} catch (e) {
					result = this.getMerchant(bus_id)
				}
			}
			return result;
		},
		//代码片段，根据商家ID获取联盟中的商家
		_getBus: function(bus_id) {
			var merchants = this.merchants || (this.merchants = []);
			var index = merchants.indexOf(bus_id);
			if (index === -1) {
				throwCusError("商家不属于指定联盟")
			}
			var bus = BusCon.get(bus_id);
			if (!bus) {
				throwCusError("找不到指定商家")
			}
			return bus;
		},
		//非安全，添加联盟商家
		_addBus: function(bus_id) {
			var bus = BusCon.get(bus_id);
			if (!bus) {
				throwCusError("找不到指定商家")
			}
			var merchants = this.merchants || (this.merchants = []);
			merchants.push(bus._id);
			this.save();
		},
		//非安全，批量添加联盟商家
		_addBusList: function(bus_ids) {
			if (bus_ids && $Array.isArray(bus_ids)) {
				bus_ids.forEach(this._addBus);
			} else {
				throwCusError("参数错误")
			}
		},
		//非安全，移除联盟商家
		_removeBus: function(bus_id) {
			var bus = this._getBus(bus_id);
			var merchants = this.merchants || (this.merchants = []);
			$Array.remove(merchants, bus_id);
			this.save();
		},
		_removeBusList: function(bus_ids) {
			if (bus_ids && $Array.isArray(bus_ids)) {
				bus_ids._removeBus(this._addBus);
			} else {
				throwCusError("参数错误")
			}
		},
		//代码片段，添加联盟成员
		_addMerchant: function(bus_id) {
			if (this.avalon === bus_id) {
				throwCusError("无需重复添加联盟盟主为联盟成员")
			}
			var merchants = this.merchants || (this.merchants = []);
			if (merchants.indexOf(bus_id) !== -1) {
				throwCusError("商家已经属于该联盟")
			}
			var bus = BusCon.get(bus_id);
			if (!bus) {
				throwCusError("找不到指定商家")
			}
			merchants.push(bus_id);
			bus._setLeague(this._id);
			this.save();
		},
		//代码片段，移除商家成员
		_removeMerchant: function(bus_id) {
			if (this.avalon === bus_id) {
				throwCusError("不可移除联盟盟主")
			}
			var merchants = this.merchants || (this.merchants = []);
			var index = merchants.indexOf(bus_id);
			if (index === -1) {
				throwCusError("所要移除商家不属于该联盟")
			}
			var bus = BusCon.get(bus_id);
			if (!bus) {
				throwCusError("找不到指定商家")
			}
			//如果是管理员尝试移除
			try {
				this._removeAdmin(bus_id);
			} catch (e) { /*console.error(e) */ }
			merchants.splice(index, 1);
			bus._setLeague(null);
			this.save();
		},
		//代码片段，添加管理员
		_addAdmin: function(bus_id) {
			if (this.avalon === bus_id) {
				throwCusError("盟主本身就是管理员")
			}
			var admins = this.admins || (this.admins = []);
			if (admins.indexOf(bus_id) !== -1) {
				throwCusError("商家已经是该联盟的管理员之一")
			}
			var merchants = this.merchants || (this.merchants = []);
			if (merchants.indexOf(bus_id) === -1) {
				throwCusError("指定成为管理员的商家不属于该联盟")
			}
			var bus = BusCon.get(bus_id);
			if (!bus) {
				throwCusError("找不到指定商家")
			}
			this.admins.push(bus_id);
			this.save();
		},
		_removeAdmin: function(bus_id) {
			var admins = this.admins || (this.admins = []);
			var index = admins.indexOf(bus_id);
			if (index === -1) {
				throwCusError("商家不属于该联盟的管理员")
			}
			admins.splice(index, 1);
			this.save();
		},
		//设置联盟成员
		_setMerchants: function(merchants) {
			//校验所有商家合理
			Object.keys(merchants).forEach(function(bus_id) {
				var bus = BusCon.get(bus_id);
				if (!bus) {
					throwCusError("找不到指定商家")
				}
			});
			this.merchants = merchants;
			this.save();
		},
		//获取联盟中所有商家成员的信息
		getMerchants: function() {
			var merchants = this.merchants || (this.merchants = []);
			if (merchants.indexOf(this.avalon) === -1) {
				merchants.unshift(this.avalon);
			}
			merchants = merchants.slice();
			merchants.forEach(function(bus_id, index) {
				merchants[index] = BusCon.get(bus_id);
			});
			return merchants;
		},
		//非安全，设置盟主
		_setAvalon: function(bus_id) {
			// var merchants = this.merchants||(this.merchants = []);
			// if (merchants.indexOf(bus_id)===-1) {
			// 	throwCusError("指定商家不属于联盟")
			// }
			var bus = BusCon.get(bus_id);
			if (!bus) {
				throwCusError("找不到指定商家")
			}
			this.avalon = bus_id;
			//双向记录盟主与所在联盟
			bus._setLeague(this._id);
			this.save();
		},
		getBuffs: function() {
			return [];
		}
	}
	module.exports = skill;

	var db = require("../db/index");
	var UserCon = require("./_con");
	var BusCon = require("../bus/_con");
	var GoodsCon = require("../goods/_con");
	var AchCon = require("../ach/_con");
	var PayAchCon = require("../pay_ach/_con");
	var CommentCon = require("../comment/_con");
	var EvaluationCon = require("../eval/_con");
	var ClassifyCon = require("../bus.classify/_con");
	var ActivityCon = require("../league.activity/_con");
	var config = require("../_include/config");
	var data_cut = require("../_include/data_cut");
	var $Array = require("../_include/$.Array");
	var $Object = require("../_include/$.Object");
	var helper = require("../_include/helper");
	var LeagueAvaloneProto = require("./avalon.skill");
	var LeagueAdminProto = require("./admin.skill");
	var LeagueMerchantProto = require("./merchant.skill");
	var db_table_name = config.dbname.league;
	var db_id_prefix = config.dbIdPrefix.league;


}());